package com.golddaymicromall.activity.profit;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.adapter.ProfitAdapter;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.ProfitInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class ProfitActivity extends BaseActivity implements View.OnClickListener {

    private ListView mProfitListView;
    private ProfitAdapter profitAdapter;
    private LinearLayout mProfitBack;
    private String logid;
    private List<ProfitInfo> listProfit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profit);
        logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        initView();
        attemptGetProfitInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.profit_back:
                finish();
                break;
        }
    }

    public void initView() {
        mProfitListView = (ListView) findViewById(R.id.profit_listview);
        mProfitBack = (LinearLayout) findViewById(R.id.profit_back);
        mProfitBack.setOnClickListener(this);
    }

    public void attemptGetProfitInfo() {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.information.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        } else {
                            listProfit = JSON.parseArray(result, ProfitInfo.class);
                            profitAdapter = new ProfitAdapter(listProfit, ProfitActivity.this);
                            mProfitListView.setAdapter(profitAdapter);
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                },throwable -> {
                    showError();
                    throwable.printStackTrace();
                });
    }
}
